package com.delivery.deliverysystem.controller;

import com.delivery.deliverysystem.common.ServerResponse;
import com.delivery.deliverysystem.exception.RException;
import com.delivery.deliverysystem.pojo.DAO.QuoteDTO;
import com.delivery.deliverysystem.pojo.VO.QuoteVO;
import com.delivery.deliverysystem.service.CommonService;
import com.delivery.deliverysystem.service.DefaultNameService;
import com.delivery.deliverysystem.service.QuoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/quote")
@Api(tags = "报价接口")
@Slf4j
public class QuoteController {

    @Autowired
    private QuoteService quoteService;
    @Autowired
    private CommonService commonService;

    @GetMapping("/getQuote")
    @ApiOperation(value = "根据类型获取报价",response = ServerResponse.class,httpMethod = "GET")
    public ServerResponse getQuote(String type){
        List<QuoteVO> quote = quoteService.getQuote(type);
        if(quote.isEmpty()){
            return ServerResponse.createByError("未查询到数据");
        }
        return ServerResponse.createBySuccess(quote);
    }

    @PostMapping("/updateQuote")
    @ApiOperation(value = "更新报价信息",response = ServerResponse.class,httpMethod = "POST")
    public ServerResponse updateQuote(@RequestBody List<QuoteDTO> quote){
        try {
            boolean b = quoteService.updateQuote(quote);
        } catch (Exception e) {
            return ServerResponse.createByError(e.getMessage());
        }
        return ServerResponse.createBySuccess("更新成功");
    }

    @PostMapping("/uploadQuoteFile")
    @ApiOperation(value = "上传报价文件",response = ServerResponse.class,httpMethod = "POST")
    public ServerResponse uploadQuoteFile(@RequestParam("file") MultipartFile file){
        String path ;
        try {
            path = commonService.uploadFile(file,"quote");
        } catch (Exception e) {
            log.error("上传报价文件出错：{}",file,e);
            return ServerResponse.createByError(e.getMessage());
        }
        return ServerResponse.createBySuccess(path);
    }

}
